//
//  BGMessageBasicController.m
//  bigMall
//
//  Created by LTOVE on 16/7/21.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGMessageBasicController.h"
#import "BGMessageContentModel.h"

#import "BGMessageBasicCell.h"

#import "BGMessageContentFrameModel.h"

#import "LTOVEDynamicCreakButton.h"

#import "BGMessageContentModel.h"
#import "BGProductIndexDataModel.h"

#import <UITableView_FDTemplateLayoutCell/UITableView+FDTemplateLayoutCell.h>

@interface BGMessageBasicController ()<dynamicButtonDelegate>{

//    NSArray *_dataArray;
//    LTOVEDynamicCreakButton *_view;
}

@end

@implementation BGMessageBasicController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
//    _btnArray = @[@"dsd"];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.tableView registerClass:[BGMessageBasicCell class] forCellReuseIdentifier:@"cell"];
//    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, BGButtonCellHeigh, 0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    GWLog(@"%lu",(unsigned long)_dataArray.count);
//    return _btnArray.count > 0 ? _dataArray.count + 1 :_dataArray.count ;
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //底部按钮cell
//    if (indexPath.row == _dataArray.count && _btnArray.count > 0) {
////        NSArray *arr = @[@"tgw",@"asd"];
//        NSDictionary *dic = @{
//                              NSFontAttributeName:Font34,
//                              NSForegroundColorAttributeName:[UIColor whiteColor]
//                              };
//        LTOVEDynamicCreakButton *view = [LTOVEDynamicCreakButton buttonViewWithFrame:CGRectMake(0, 0, screenW, BGMessageBtn) titleDataArray:_btnArray andCol:(int)_btnArray.count andEdgInset:UIEdgeInsetsMake(10,BGMaginLeft , 10, BGMaginLeft) andMaginX:10 andMaginY:0 andBtnHeigh:45 andBtnNorColor:GWColor(223, 26, 25, 1) andBtnHeighColor:GWColor(223, 26, 25, 1) andRadius:7 andFont:dic];
//        view.delegate = self;
//        //        cell.backgroundView = view;
//        UITableViewCell * cell = [UITableViewCell new];
//        cell.selected = NO;
////        cell.userInteractionEnabled = NO;
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        [cell.contentView addSubview:view];
//        return cell;
//    }
    //正常的cell
    BGMessageBasicCell *cell = [BGMessageBasicCell cellWithTableView:tableView IndexPath:indexPath];
    if (self.dataArray.count < indexPath.row) {
        return cell;
    }
    NSLog(@"ceceeceececec----%@++++++++++++%ld",self.dataArray,(long)indexPath.row);
    NSInteger  tap;
    if (self.dataArray.count <= indexPath.row) {
        tap = self.dataArray.count - 1;
    }else
    {
        tap = indexPath.row;
    }
    
    BGMessageContentFrameModel *Fmodel = self.dataArray[tap];
    
    Fmodel.unit = self.unit == nil ? @"" : self.unit;
    cell.Fmodel = Fmodel;
    return cell;
}

- (void)viewWillAppear:(BOOL)animated
{
    [self addDynamicBtn];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.views removeFromSuperview];
//    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}

//- (void)setBtnArray:(NSArray *)btnArray
//{
//    _btnArray = btnArray;
//    if (self.isRect) {
//        [_view removeFromSuperview];
//        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, - BGButtonCellHeigh, 0);
//        [self addDynamicBtn];
//    }
//    
//}

- (void)addDynamicBtn
{
    NSDictionary *dic = @{
                          NSFontAttributeName:Font34,
                          NSForegroundColorAttributeName:[UIColor whiteColor]
                          };
//    NSLog(@"%@",_btnArray);
    CGFloat x = BGButtonCellHeigh;
    CGFloat heigh = screenH - x;
    LTOVEDynamicCreakButton *view = [LTOVEDynamicCreakButton buttonViewWithFrame:CGRectMake(0, heigh, screenW, BGButtonCellHeigh) titleDataArray:_btnArray andCol:(int)_btnArray.count andEdgInset:UIEdgeInsetsMake(10,BGMaginLeft , 10, BGMaginLeft) andMaginX:10 andMaginY:0 andBtnHeigh:BGMessageBtn andBtnNorColor:read_ andBtnHeighColor:read_ andRadius:7 andFont:dic];
//    LTOVEDynamicCreakButton *view = [LTOVEDynamicCreakButton buttonViewWithFrame:CGRectMake(0,heigh, screenW, BGButtonCellHeigh) titleDataArray:self.btnArray andEdgInset:UIEdgeInsetsMake(10, BGMaginLeft, 10, BGMaginLeft) andMaginX:0 andMaginY:0 andBtnHeigh:BGMessageBtn andBtnNorColor:read_ andBtnHeighColor:read_ andRadius:7 andFont:dic];
//    view.delegate = self;
//    view.y = 300;
    self.views = view;
    view.delegate = self;
//    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, BGButtonCellHeigh, 0);
    [BGKeyWindow addSubview:view];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"%@",indexPath);
    if (_dataArray.count == indexPath.row) {
        return;
    }
    BGMessageContentFrameModel *Fmodel = self.dataArray[indexPath.row];
    
    BGMessageContentModel *model = Fmodel.dataModel;
    if (model.option) {
        NSArray *aa = @[@"dsds"];
        model.option(aa);
        return;
    }
    if (model.descVc) {
        UIViewController *vc = [[model.descVc alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
    
#warning 点击cell BGProductIndexDataModel
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row > self.dataArray.count -1) {
        return 45;
    }
    BGMessageContentFrameModel *Fmodel = self.dataArray[indexPath.row];
    if ([Fmodel.dataModel.type isEqualToString:@"tbjl"]) {
        return 45;
    }
    if ([Fmodel.dataModel isKindOfClass:[BGAuctionDetailsDataModel class]]) {
        
        return [tableView fd_heightForCellWithIdentifier:@"cell" configuration:^(BGMessageBasicCell *cell) {
            Fmodel.unit = self.unit == nil ? @"" : self.unit;
            cell.Fmodel = Fmodel;
            
        }];
    }
    
    
   
    if (indexPath.row == _dataArray.count && _btnArray.count > 0) {
        return BGButtonCellHeigh;
    }
    NSInteger  tap;
    if (self.dataArray.count <= indexPath.row) {
        tap = self.dataArray.count - 1;
        return 0;
    }else
    {
        tap = indexPath.row;
        BGMessageContentFrameModel *Fmodel = self.dataArray[indexPath.row];
        return Fmodel.cellHeigh;
    }
    
    
}


- (void)dealloc
{
    NSLog(@"xiaoshi");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"timerInvited" object:nil];
}

@end
